package cn.cj.erp.modules.system.controller;

import cn.cj.erp.bo.system.SysConfigBo;
import cn.cj.erp.common.excel.util.ExcelUtil;
import cn.cj.erp.common.security.annotation.Log;
import cn.cj.erp.common.enums.LogBusinessTypeEnum;
import cn.cj.erp.common.Page;
import cn.cj.erp.common.http.Result;
import cn.cj.erp.common.utils.ServletUtils;
import cn.cj.erp.modules.common.controller.AbstractController;
import cn.cj.erp.service.system.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 系统配置信息
 *
 * @author chenjian
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends AbstractController {
    @Resource
    private SysConfigService sysConfigService;

    /**
     * 所有配置列表
     */
    @PostMapping("/list")
    @PreAuthorize("@ss.hasPerm('system:config:list')")
    public Result<Page<SysConfigBo>> list(@RequestBody SysConfigBo params) {
        return Result.query(sysConfigService.queryPage(params));
    }

    /**
     * 配置信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPerm('system:config:query')")
    public Result<SysConfigBo> info(@PathVariable("id") Long id) {
        return Result.query(sysConfigService.getById(id));
    }

    /**
     * 保存配置
     */
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('system:config:add')")
    @Log(title = "参数管理", businessType = LogBusinessTypeEnum.INSERT)
    public Result<Boolean> save(@Valid @RequestBody SysConfigBo config) {
        return Result.save(sysConfigService.save(config));
    }

    /**
     * 修改配置
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPerm('system:config:edit')")
    @Log(title = "参数管理", businessType = LogBusinessTypeEnum.UPDATE)
    public Result<Boolean> update(@Valid @RequestBody SysConfigBo config) {
        return Result.update(sysConfigService.updateById(config));
    }

    /**
     * 删除配置
     */
    @PostMapping("/delete/{ids}")
    @PreAuthorize("@ss.hasPerm('system:config:remove')")
    @Log(title = "参数管理", businessType = LogBusinessTypeEnum.DELETE)
    public Result<Boolean> delete(@PathVariable("ids") Long[] ids) {
        return Result.remove(sysConfigService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public Result<String> getConfigKey(@PathVariable String configKey) {
        return Result.query(sysConfigService.getValue(configKey));
    }

    /**
     * 刷新缓存
     */
    @PreAuthorize("@ss.hasPerm('system:config:remove')")
    @Log(title = "参数管理", businessType = LogBusinessTypeEnum.CLEAN)
    @PostMapping("/refreshCache")
    public Result<Boolean> refreshCache() {
        return Result.ok(sysConfigService.refreshCache());
    }

    @Log(title = "参数管理", businessType = LogBusinessTypeEnum.EXPORT)
    @PreAuthorize("@ss.hasPerm('system:config:export')")
    @PostMapping("/export")
    public void export(@RequestBody SysConfigBo params) {
        List<SysConfigBo> list = sysConfigService.queryList(params);
        ExcelUtil.exportExcel(SysConfigBo.class, list, ServletUtils.getResponse(), "系统配置");
    }

    /**
     * 校验手机是否唯一
     */
    @PreAuthorize("@ss.hasPerm('system:config:edit')")
    @GetMapping("/checkConfigKeyUnique")
    public Result<Boolean> checkConfigKeyUnique(SysConfigBo vo) {
        return Result.ok(sysConfigService.keyNotExist(vo));
    }
}
